<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/6 0006
 * Time: 下午 1:58
 */

namespace App\Services;


use App\Permission;
use App\Role;

class AdminService extends Service
{
    /**
     * @description:权限列表
     * @author: hkw <hkw925@qq.com>
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function permissionTree(){
        $permission = new Permission();
        $list = $permission->orderBy('sort', 'asc')->get()->toArray();
        $listTree = list_to_tree($list);
        return $this->success('权限列表',$listTree);
    }

    /**
     * @description:更新树形菜单
     * @author: hkw <hkw925@qq.com>
     * @param array $input
     */
    public function updatePermissionTree(array $input){
        $permission = new Permission();
        $permissionTree = json_decode($input['list'],true);
        foreach($permissionTree as $k=>$v){
            $permission->where('id',$v['id'])->update(['sort'=>$k,'pid'=>0]);
            if(!empty($v['children'])){
                foreach($v['children'] as $k1=>$v1){
                    $permission->where('id',$v1['id'])->update(['sort'=>$k1,'pid'=>$v['id']]);
                    if(!empty($v1['children'])){
                        foreach($v1['children'] as $k2=>$v2){
                            $permission->where('id',$v2['id'])->update(['sort'=>$k2,'pid'=>$v1['id']]);
                        }
                    }
                }
            }

        }
        service('Route')->updateRouteCache();
    }

    /**
     * @description:更新超级管理员权限
     * @author: hkw <hkw925@qq.com>
     */
    public function updateAdminPermission(){
        $permission = new Permission();
        $allPermission = $permission->pluck('id')->toArray();
        $admin = Role::where('name', '=', 'admin')->first();
        $admin->perms()->sync($allPermission);
    }

    /**
     * @description:权限新增
     * @author: hkw <hkw925@qq.com>
     * @param array $input
     * @return bool
     */
    public function permissionAdd(array $input){
        $permission = new Permission();
        $permission->name = $input['name'];
        $permission->pid = $input['pid'];
        $permission->display_name = $input['display_name'];
        $permission->description = $input['description'];
        $permission->type = $input['type'];
        $permission->icon = $input['icon'];
        $permission->controller = $input['controller'];
        $permission->method = $input['method'];
        $permission->sort = $permission->where(['pid'=>$input['pid']])->max('sort')?:0;
        $result = $permission->save();
        service('Route')->updateRouteCache();
        $this->updateAdminPermission();
        return $result;
    }

    /**
     * @description:初始化权限数据
     * @author: hkw <hkw925@qq.com>
     */
    public function initPermission(){
        $permission = new Permission();
        $permission->insert([
            [
                'id'=>1,
                'name'=>'/info',
                'pid'=>0,
                'sort'=>0,
                'type'=>2,
                'icon'=>'fa-home',
                'controller'=>'Index',
                'method'=>'info',
                'display_name'=>'后台首页',
                'description'=>'首页数据统计'
            ],
            [
                'id'=>2,
                'name'=>'/test',
                'pid'=>0,
                'sort'=>1,
                'type'=>2,
                'icon'=>'fa-table',
                'controller'=>'0',
                'method'=>'0',
                'display_name'=>'测试模块',
                'description'=>'测试模块'
            ],[
                'id'=>3,
                'name'=>'/permission',
                'pid'=>0,
                'sort'=>2,
                'type'=>1,
                'icon'=>'fa-users',
                'controller'=>'0',
                'method'=>'0',
                'display_name'=>'权限管理',
                'description'=>'权限管理'
            ],
            [
                'id'=>4,
                'name'=>'/permission',
                'pid'=>3,
                'sort'=>0,
                'type'=>2,
                'icon'=>'',
                'controller'=>'Admin',
                'method'=>'permission',
                'display_name'=>'权限列表',
                'description'=>'权限列表'
            ],[
                'id'=>5,
                'name'=>'/roles',
                'pid'=>3,
                'sort'=>1,
                'type'=>1,
                'icon'=>'',
                'controller'=>'Admin',
                'method'=>'roles',
                'display_name'=>'角色列表',
                'description'=>'角色列表'
            ],[
                'id'=>6,
                'name'=>'/users',
                'pid'=>3,
                'sort'=>2,
                'type'=>1,
                'icon'=>'',
                'controller'=>'Admin',
                'method'=>'users',
                'display_name'=>'管理员列表',
                'description'=>'管理员列表'
            ],[
                'id'=>7,
                'name'=>'/test',
                'pid'=>2,
                'sort'=>0,
                'type'=>1,
                'icon'=>'',
                'controller'=>'Test',
                'method'=>'index',
                'display_name'=>'测试列表',
                'description'=>'用于测试'
            ],
            [
                'id'=>8,
                'name'=>'/permissionAdd',
                'pid'=>4,
                'sort'=>0,
                'type'=>1,
                'icon'=>'',
                'controller'=>'Admin',
                'method'=>'permissionAdd',
                'display_name'=>'权限添加',
                'description'=>'权限添加'
            ],
            [
                'id'=>9,
                'name'=>'/updatePermission',
                'pid'=>4,
                'sort'=>1,
                'type'=>3,
                'icon'=>'',
                'controller'=>'Admin',
                'method'=>'updatePermission',
                'display_name'=>'更新排序',
                'description'=>'更新排序'
            ]
        ]);
        $this->updateAdminPermission();
        service('Route')->updateRouteCache();
    }
}